package com.one.tank;

import java.awt.*;

//子弹
public class Bullet {
    //子弹状态常量 0-空闲 1-运行
    public static final int BULLET_STATE_FREE = 0;
    public static final int BULLET_STATE_RUNNING = 1;
    //子弹攻击力常量 最大和最小攻击力
    public static final int BULLET_MAX_ATTACK = 100;
    public static final int BULLET_MIN_ATTACK = 20;
    //子弹左上角X坐标
    private int x;
    //子弹左上角Y坐标
    private int y;
    //子弹宽度
    private int width = 10;
    //子弹高度
    private int height = 10;
    //子弹方向
    private int direction;
    //子弹速度
    private int speed = 16;
    //子弹状态
    private int state = BULLET_STATE_RUNNING;
    //坦克类型
    private Tank tank;
    //子弹攻击力
    private int attack;

    /**
     * 构造函数
     *
     * @param tank 所属坦克
     */
    public Bullet(Tank tank) {
        this.tank = tank;
        this.attack = GameLogic.getRandomInt(BULLET_MIN_ATTACK, BULLET_MAX_ATTACK);
    }

    /**
     * 设置子弹位置
     */
    public void setPosition(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    /**
     * 功能：子弹重置
     */
    public void reSet() {
        this.setX(-100);
        this.setY(-100);

        //重新随机生成攻击力
        this.attack = GameLogic.getRandomInt(BULLET_MIN_ATTACK, BULLET_MAX_ATTACK);
        this.state = BULLET_STATE_FREE;
    }

    /**
     * 让子弹飞一会
     */
    public void fly() {
        //设置子弹的状态
        this.state = BULLET_STATE_RUNNING;
        //启动线程让子弹飞
        new Thread(() -> {
            while (this.state == BULLET_STATE_RUNNING) {
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                switch (direction) {
                    case Tank.UP:
                        if (this.y - this.speed >= 0) {
                            this.y -= this.speed;
                        } else {
                            this.reSet();
                        }
                        break;
                    case Tank.DOWN:
                        if (this.y + this.height + this.speed <= GamePanel.GAME_HEIGHT) {
                            this.y += this.speed;
                        } else {
                            this.reSet();
                        }
                        break;
                    case Tank.LEFT:
                        if (this.x - this.speed >= 0) {
                            this.x -= this.speed;
                        } else {
                            this.reSet();
                        }
                        break;
                    case Tank.RIGHT:
                        if (this.x + this.width + this.speed <= GamePanel.GAME_WIDTH) {
                            this.x += this.speed;
                        } else {
                            this.reSet();
                        }
                        break;
                }
            }
        }).start();
    }
    //画子弹
    public void drawBullet(Graphics g) {
        //我方坦克子弹
        if (tank.getType() == Tank.TANK_TYPE_OUR) {
            g.setColor(Tank.OUR_TANK_COLOR);
        }
        //敌方坦克子弹
        else {
            g.setColor(Tank.ENEMY_TANK_COLOR);
        }
        g.fillOval(this.x, this.y, this.width, this.height);
        //根据攻击力画加强子弹
        g.setColor(Color.RED);
        if (this.attack == 100) {
            g.fillOval(this.x, this.y, this.width, this.height);
        } else if (this.attack >= 90) {
            g.fillOval(this.x + 1, this.y + 1, this.width - 2, this.height - 2);
        } else if (this.attack >= 70) {
            g.fillOval(this.x + 2, this.y + 2, this.width - 4, this.height - 4);
        } else if (this.attack >= 50) {
            g.fillOval(this.x + 3, this.y + 3, this.width - 6, this.height - 6);
        }
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getDirection() {
        return direction;
    }

    public int getSpeed() {
        return speed;
    }

    public int getState() {
        return state;
    }

    public int getAttack() {
        return attack;
    }
}
